package com.app.ui.activity
import android.annotation.SuppressLint
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.app.NewsApplication.Companion.if_login
import com.app.NewsApplication.Companion.loginUser
import com.app.R
import com.app.model.User
import com.app.util.DBOpenHelper
import java.lang.Exception
import java.util.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_user.*

class LoginActivity : AppCompatActivity(), View.OnClickListener {
    private var mDBOpenHelper: DBOpenHelper? = null
    private var et_User: EditText? = null
    private var et_Psw: EditText? = null
    private var cb_rmbPsw: CheckBox? = null
    private var userName: String? = null
    private var editor: SharedPreferences.Editor? = null

    @SuppressLint("CommitPrefEdits")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_login)
        initView() //初始化界面
        mDBOpenHelper = DBOpenHelper(this)
        val sp = getSharedPreferences("user_mes", MODE_PRIVATE)
        editor = sp.edit()
        if (sp.getBoolean("flag", false)) {
            val user_read = sp.getString("user", "")
            val psw_read = sp.getString("psw", "")
            et_User!!.setText(user_read)
            et_Psw!!.setText(psw_read)
        }
    }

    private fun initView() {
        //初始化控件
        et_User = findViewById(R.id.et_User)
        et_Psw = findViewById(R.id.et_Psw)
        cb_rmbPsw = findViewById(R.id.cb_rmbPsw)
        val btn_Login = findViewById<Button>(R.id.btn_Login)
        val tv_register = findViewById<TextView>(R.id.tv_Register)
        //设置点击事件监听器
        btn_Login.setOnClickListener(this)
        tv_register.setOnClickListener(this)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.tv_Register -> {
                val intent = Intent(this@LoginActivity, RegisteredActivity::class.java) //跳转到注册界面
                startActivity(intent)
                finish()
            }
            R.id.btn_Login -> {
                val name = et_User!!.text.toString().trim { it <= ' ' }
                val password = et_Psw!!.text.toString().trim { it <= ' ' }
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(password)) {
                    val data: ArrayList<User> = mDBOpenHelper!!.allData
                    var match = false
                    var match2 = false
                    var i = 0
                    while (i < data.size) {
                        val user: User = data[i]
                        if (name == user.name && password == user.password ||
                            name == user.email && password == user.password ||
                            name == user.phonenum && password == user.password
                        ) {
                            userName = user.name
                            match = true
                            match2 = if (cb_rmbPsw!!.isChecked) {
                                editor!!.putBoolean("flag", true)
                                editor!!.putString("user", user.name)
                                editor!!.putString("psw", user.password)
                                editor!!.apply()
                                true
                            } else {
                                editor!!.putString("user", user.name)
                                editor!!.putString("psw", "")
                                //                                editor.clear();
                                editor!!.apply()
                                false
                            }
                            break
                        } else {
                            match = false
                        }
                        i++
                    }
                    if (match) {
                        if (match2) {
                            cb_rmbPsw!!.isChecked = true
                        }
                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show()
                        //改变全局变量的值
                        if_login = 1
                        var target: Runnable
                        //用线程启动
                        val thread: Thread = object : Thread() {
                            override fun run() {
                                try {
//                                    sleep(1000) //1秒 模拟登录时间
                                    val user_name = userName
                                    val intent1 = Intent(
                                        this@LoginActivity,
                                        MainActivity::class.java
                                    ) //设置自己跳转到成功的界面
                                    loginUser = user_name.toString();
                                    intent1.putExtra("user_name",user_name)
                                    startActivity(intent1)
                                    finish()
                                } catch (e: Exception) {
                                    e.printStackTrace()
                                }
                            }
                        }
                        thread.start() //打开线程
                    } else {
                        Toast.makeText(this, "用户名或密码不正确，请重新输入", Toast.LENGTH_SHORT).show()
                    }
                } else {
                    Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }
}
